package com.example.gcmtest1.httpserver;

import java.io.File;
import java.util.ArrayList;

import com.example.gcmtest1.MySqlHelper;
import com.example.gcmtest1.youtube.MyRandomWriter;
import com.example.gcmtest1.youtube.SmartHTTPDownload;
import com.example.gcmtest1.youtube.YoutubeDownloader;
import com.example.gcmtest1.youtube.YoutubeSQLRecord;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class HTTPProxyService extends Service {
	
	public static boolean REFRESH_DATABASE = true;
	public static String DATABASE_FILE = "mydb1.db";

	private static HTTPProxyService sInstance;
	
	public static boolean ISRUNNING = false;
	private String logName = "HTTPProxyService";
	public MySqlHelper sqlHelper;
		
	public ArrayList<SmartHTTPDownload> arrDownloading;
	public static ArrayList<Object> fileListItems;
	
	public HTTPProxyService() {
		Log.d(logName, "Class initialized()");
		
		HTTPProxyService.fileListItems = new ArrayList<Object>();
		this.arrDownloading = new ArrayList<SmartHTTPDownload>();

	}
	
	
    @Override
    public void onCreate() {
    	
        super.onCreate();
        Log.d(logName, "onCreate()");
        
        this.manageDatabase();
    }
    
	
	private void manageDatabase() {
		String currentDBFile = this.getDatabasePath(DATABASE_FILE).getAbsolutePath();
        Log.d(logName, "searching for existing " + DATABASE_FILE + " first, at "  + currentDBFile);
		
        File f = new File(this.getDatabasePath(DATABASE_FILE).getAbsolutePath());
        if (f.exists() == false) {
        	Log.e(logName, "Cannot find database at " + currentDBFile);
        	try {
        		MyRandomWriter.copyFile(this.getAssets().open(DATABASE_FILE), currentDBFile);
        		Log.d(logName, "First copied file from Asset to database location at " + currentDBFile);
        	} catch (Exception e) {
        		Log.e(logName, "Cannot copy file from Asset");
        		e.printStackTrace();
        	}
        	
        } else {
        	Log.i(logName, "REFRESH_DATABASE=" + REFRESH_DATABASE);
        	Log.d(logName, "Found existing database at " + currentDBFile);
        	
        	if (REFRESH_DATABASE == true) {        		
        		boolean b = f.delete();
            	if (b == true) {
            		Log.d(logName, "deleted from " + currentDBFile);
            		try {
            			MyRandomWriter.copyFile(this.getAssets().open(DATABASE_FILE), currentDBFile);
                		Log.d(logName, "Replaced database from Asset done");	
            		} catch (Exception e) {
            			Log.e(logName, "Cannot replace database");
                		e.printStackTrace();	
            		}
            		
            	}	
        	} else {
        		Log.i(logName, "Did not replace Database from Asset");
        	}
        	
        }		
	}
	
	public void startDownload(String finalUrl, String fileName, String firstUrl) {
		YoutubeDownloader downloader = new YoutubeDownloader(this, firstUrl);
		downloader.execute("1", finalUrl, fileName);
		
		
	}
	
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    	Log.d(logName, "Service started");
    	
    	WebServer ws = new WebServer(this);
    	ws.startThread();
    	ISRUNNING = true;
    	
    	sInstance = this;
    	this.sqlHelper = new MySqlHelper(this);
    	
    	return START_STICKY;
    }
    
    public static HTTPProxyService getInstance() {
    	return(sInstance);
    }
        
    @Override
    public void onDestroy() {
    	Log.d(logName, "onDestroy()");
    }
	
	@Override
	public IBinder onBind(Intent intent) {

		return null;
	}

}
